layui.config({
        base: '/static/js/lib',
    version: new Date().getTime()
}).use(['layer', "table"], function () {

    $ = layui.jquery;
    var layer = layui.layer;
    var table = layui.table;
    var form = layui.form;
    form.on('submit(sreach)', function (data) {
        tableRender(data.field);
        return false;
    });

    function tableRender(queryCondition) {
        var url = "/course/page?";
        for (var key in queryCondition) {
            var condition = queryCondition[key];
            if (null != condition && condition != "") {
                url += ("&" + key + "=" + condition);
            }
        }

        table.render({
            elem: '#course-page',
            height: 472,
            url: url,
            page: true,
            cols: [[ //表头
                {field: 'id', title: 'ID', width: "10%", fixed: 'left'}
                , {field: 'courseName', title: '课程名称', width: "30%"}
                , {field: 'passPercent', title: '课程通过百分比', width: "10%"}
                , {field: 'countPriceType', title: '计价类型', width: "8%"}
                , {field: 'studyDay', title: '课程有效期', width: "8%"}
                , {field: 'originalPrice', title: '课程原价', width: "10%"}
                , {field: 'salePrice', title: '课程售卖价', width: "10%"}
                , {title: "操作", width: "14%", toolbar: '#funcBar'}
            ]]
        });
    }

    tableRender({});

    $("#cateName").focus(function () {
        layer.open({
            type: 2,
            area: ['400px', '500px'],
            maxmin: true,
            moveOut: false,
            shade: 0.4,
            async: true,
            title: '选择课程分类',
            content: "/courseCate/tree"
        });
    });

    window.cateTreeCallBack = function (node) {
        if (node.isSubCate) {
            $("#cateName").val(node.name);
            $("input[name='cateId']").val(node.id);
            $("input[name='cateIdPath']").val(node.catePath);
            return {isClose: true};
        }
        return {
            isClose: false,
            callback: function () {
                layer.msg("只能选择子分类");
            }
        };
    };
    // 跳转到添加课程页面
    $(".course-add").click(function () {
        layer.open({
            type: 2,
            area: ['100%', '100%'],
            fix: false,
            maxmin: true,
            shadeClose: true,
            shade: 0.4,
            title: "数据中心",
            content: "/course/all/date/page",
            end: function () {
                location.replace(location.href);
            }
        })
    });

    //监听工具条
    table.on('tool(course-page)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        var tr = obj.tr;
        if (layEvent == "edit") {
            layer.open({
                type: 2,
                area: ['100%', '100%'],
                fix: false,
                maxmin: true,
                shadeClose: true,
                shade: 0.4,
                title: "编辑课程",
                content: "/course/edit/view?id=" + data.id,
                end: function () {
                    location.replace(location.href);
                }
            });
        } else if (layEvent == "outline") {
            layer.open({
                type: 2,
                area: ['100%', '100%'],
                fix: false,
                maxmin: true,
                shadeClose: true,
                shade: 0.4,
                title: "课程大纲",
                content: "/chapter/view?id=" + data.id
            });
        } else if (layEvent == "del") {
            layer.confirm("确定要删除吗？", {icon: 0, title: '提示'}, function () {
                $.get("/course/del?id=" + data.id, function (res) {
                    if (res.code == "00000") {
                        layer.msg("删除成功", {
                            icon: 6, time: 1500, end: function () {
                                obj.del();
                            }
                        });
                        location.replace(location.href);
                    } else if (res.code == "000006") {
                        layer.msg(res.desc, {icon: 5, time: 2000});
                    } else {
                        console.log(res);
                    }
                });
            });
        }
    });
});